﻿<?php
require('configs/include.php');

class c_enviar_programa extends super_controller {

    public function enviar() {
        $programadorLogueado = $_SESSION['usuario']['username'];
        $programasSeleccionados = $this->post->programas;
        if (count($programasSeleccionados) > 0) {

            $options['programa']['lvl2'] = "by_programador";
            $cod['programa']['id_programador'] = $_SESSION['usuario']['cedula'];
            $this->orm->connect();
            $this->orm->read_data(array("programa"), $options, $cod);
            $programasEnBD = $this->orm->get_objects("programa");
            for ($i = 0; $i < count($programasSeleccionados); $i++) {
                $temp = 0;
                for ($j = 0; $j < count($programasEnBD); $j++) {
                    if ($programasEnBD[$j]->get('id') == $programasSeleccionados[$i]) {
                        $temp = $temp + 1;
                    }
                }
                if ($temp == 1) {
                    $newPrograma = new programa();
                    $newPrograma->set('id', $programasSeleccionados[$i]);
                    $this->orm->update_data("by_idprogram", $newPrograma);
                }
            }
        } else {

            $this->type_warning = "error";
            $this->msg_warning = "Debe seleccionar almenos un programa";
            $this->img_warning = "incorrect.png";
            $this->engine->assign('img_warning', $this->img_warning);
            $this->engine->assign('msg_warning', $this->msg_warning);
            $this->temp_aux = 'message.tpl';
        }
    }

    public function display() {

        $varAux = $this->gvar['n_index'] . " -> " . $this->gvar['n_program'];
        $this->engine->assign('ubicationPath', $varAux);
        $typeSession = $_SESSION['usuario']['tipo'];
        $this->engine->display('header.tpl');
        $this->engine->display($this->temp_aux);
        if ($typeSession == "lider" || $typeSession == "programador" || $typeSession == "intermediario") {
            $options['programa']['lvl2'] = "by_programador";
            $cod['programa']['id_programador'] = $_SESSION['usuario']['cedula'];
            $this->orm->connect();
            $this->orm->read_data(array("programa"), $options, $cod);
            $programas = $this->orm->get_objects("programa");
            if (count($programas) > 0) {
                $this->engine->assign('programas', $programas);
            } else {
                $this->type_warning = "error";
                $this->msg_warning = "No tiene programas listos para enviar";
                $this->img_warning = "incorrect.png";
                $this->engine->assign('img_warning', $this->img_warning);
                $this->engine->assign('msg_warning', $this->msg_warning);
                $this->temp_aux = 'message.tpl';
            }
            $this->engine->display('programSend.tpl');
        } else {
            $this->engine->display('sinPermisos.tpl');
        }
        $this->engine->display($this->temp_aux);
        $this->engine->display('footer.tpl');
    }

    public function run() {


        try {
            if (isset($this->get->option)) {
                $this->{$this->get->option}();
            }
        } catch (Exception $e) {
            $this->error = 1;
            $this->msg_warning = $e->getMessage();
            $this->img_warning = "incorrect.png";
            $this->engine->assign('type_warning', $this->type_warning);
            $this->engine->assign('msg_warning', $this->msg_warning);
            $this->engine->assign('img_warning', $this->img_warning);
            $this->temp_aux = 'message.tpl';
        }
        $this->display();
    }

}

$call = new c_enviar_programa();
$call->run();
?>